package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.dblib.dao.impl.LocationDao;
import com.qf.flash.R;
import com.qf.flash.model.MyCollectShopBean;
import com.qf.flash.mvp.contract.MyCollectShopContract;
import com.qf.flash.mvp.presenter.MyCollectShopPresenter;
import com.qf.flash.ui.home.adapter.CollectShopItem;
import com.qf.flash.utils.LogUtil;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.BaseActivity;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我收藏的商家
 * 未完成-
 */
public class MyCollectedShopActivity extends BaseActivity implements MyCollectShopContract.View, CollectShopItem.CheckedListener, OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.rv_frame)
    RecyclerView mRecyclerView;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.iv_bottom_check)
    ImageView ivBottomCheck;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    @BindView(R.id.tv_delete_collect)
    TextView tvDeleteCollect;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;
    private boolean isShow;
    private RVAdapter<Object> mAdapter;
    private MyCollectShopPresenter collectShopPresenter;
    private CollectShopItem collectShopItem;
    private List<Object> datas;
    private boolean checkAll;
    private int page = 1, size = 30;
    private String lats;
    private String lngs;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_collect_shop;
    }

    @Override
    protected void initInject() {
        super.initInject();
        collectShopPresenter = new MyCollectShopPresenter();
        initPresenters(collectShopPresenter);
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("我收藏的商家");
        tvTopRight.setText("管理");
        //   -------------------setCheckedListener
        collectShopItem = new CollectShopItem();
        mAdapter = new RVAdapter<>(collectShopItem);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);

        double lat = LocationDao.getInstance().getLocationBean().getLat();
        double lng = LocationDao.getInstance().getLocationBean().getLng();
        DecimalFormat format = new DecimalFormat("#.000000");
        lats = format.format(lat);
        lngs = format.format(lng);
        collectShopItem.setCheckedListener(this);

        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);
        mRefresh.autoRefresh();
    }


    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MyCollectedShopActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_top_right, R.id.iv_bottom_check, R.id.tv_delete_collect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_top_right:

                if (isShow) {
                    tvTopRight.setText("管理");
                    llBottom.setVisibility(View.GONE);
                    isShow = !isShow;
                } else {
                    tvTopRight.setText("完成");
                    llBottom.setVisibility(View.VISIBLE);
                    isShow = !isShow;
                }
                for (Object data : datas) {
                    MyCollectShopBean bean = (MyCollectShopBean) data;
                    bean.isShow = isShow;
                    bean.isChecked = false;
                }
                mAdapter.notifyDataSetChanged();
                ivBottomCheck.setImageResource(R.mipmap.icon_uncheck);
                checkAll = false;
                break;
            case R.id.iv_bottom_check:// 全选
                if (checkAll) {
                    ivBottomCheck.setImageResource(R.mipmap.icon_uncheck);
                    for (Object data : datas) {
                        MyCollectShopBean bean = (MyCollectShopBean) data;
                        bean.isChecked = false;
                    }
                } else {
                    ivBottomCheck.setImageResource(R.mipmap.icon_check);
                    for (Object data : datas) {
                        MyCollectShopBean bean = (MyCollectShopBean) data;
                        bean.isChecked = true;
                    }
                }
                mAdapter.notifyDataSetChanged();
                checkAll = !checkAll;
                break;
            case R.id.tv_delete_collect:// 取消收藏

                String deleteIds = "";
                int index = 0;
                for (int i = 0; i < datas.size(); i++) {
                    MyCollectShopBean bean = (MyCollectShopBean) datas.get(i);
                    if (bean.isChecked) {
                        index++;
                        deleteIds = deleteIds + "," + bean.getShopId();
                    }
                    bean.isShow = false;
                }
                String replace = "";
                if (index > 0) {
                    replace = deleteIds.substring(1, deleteIds.length());
                }
                LogUtil.d("deleteIds:" + replace);

                collectShopPresenter.deleteCollectShop(replace);
                break;
            default:
                break;
        }
    }

    @Override
    public void setChecked(int position, boolean ischecked) {
        MyCollectShopBean collectBean = (MyCollectShopBean) datas.get(position);
        collectBean.isChecked = !ischecked;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void setUi(List<MyCollectShopBean> collectShopBeans, int totalPage) {
        mRefresh.setEnableLoadMore(page < totalPage);
        datas = mAdapter.getDatas();
        if (isRefresh) {
            mRefresh.finishRefresh();
            datas.clear();
        } else if (isLoadMore) {
            mRefresh.finishLoadMore();
        }
        /*List<MyCollectShopBean> collectBeans = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            MyCollectShopBean bean = new MyCollectShopBean();
            bean.setId("11"+i+"0");
            collectBeans.add(bean);
        }
        datas.addAll(collectBeans);*/

        if (collectShopBeans == null)
            return;
        datas.addAll(collectShopBeans);

        mAdapter.notifyDataSetChanged();
        isRefresh = false;
        isLoadMore = false;
    }

    @Override
    public void deletedOver() {
        mRefresh.autoRefresh();
        tvTopRight.setText("管理");
        ivBottomCheck.setImageResource(R.mipmap.icon_uncheck);
        llBottom.setVisibility(View.GONE);
        isShow = false;
    }

    private boolean isRefresh, isLoadMore;

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        page = 1;
        size = 30;
        collectShopPresenter.getCollectShopList(lngs, lats, page, size);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isLoadMore = true;
        collectShopPresenter.getCollectShopList(lngs, lats, ++page, ++size);
    }
}
